1 /* iowin32.c -- IO base function header for compress/uncompress .zip
2 files using zlib + zip or unzip API
3 This IO API version uses the Win32 API (for Microsoft Windows)
5 Version 1.01e, February 12th, 2005
7 Copyright (C) 1998-2005 Gilles Vollant
12 #include "ZLIB/zlib.h"
16 #ifndef INVALID_HANDLE_VALUE
17 #define INVALID_HANDLE_VALUE (0xFFFFFFFF)
20 #ifndef INVALID_SET_FILE_POINTER
21 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
24 voidpf ZCALLBACK win32_open_file_func
OF((
29 uLong ZCALLBACK win32_read_file_func
OF((
35 uLong ZCALLBACK win32_write_file_func
OF((
41 long ZCALLBACK win32_tell_file_func
OF((
45 long ZCALLBACK win32_seek_file_func
OF((
51 int ZCALLBACK win32_close_file_func
OF((
55 int ZCALLBACK win32_error_file_func
OF((
65 voidpf ZCALLBACK
win32_open_file_func (opaque
, filename
, mode
)
70 const char* mode_fopen
= NULL
;
71 DWORD dwDesiredAccess
,dwCreationDisposition
,dwShareMode
,dwFlagsAndAttributes
;
75 dwDesiredAccess
= dwShareMode
= dwFlagsAndAttributes
= 0;
77 if ((mode
& ZLIB_FILEFUNC_MODE_READWRITEFILTER
)==ZLIB_FILEFUNC_MODE_READ
)
79 dwDesiredAccess
= GENERIC_READ
;
80 dwCreationDisposition
= OPEN_EXISTING
;
81 dwShareMode
= FILE_SHARE_READ
;
84 if (mode
& ZLIB_FILEFUNC_MODE_EXISTING
)
86 dwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
87 dwCreationDisposition
= OPEN_EXISTING
;
90 if (mode
& ZLIB_FILEFUNC_MODE_CREATE
)
92 dwDesiredAccess
= GENERIC_WRITE
| GENERIC_READ
;
93 dwCreationDisposition
= CREATE_ALWAYS
;
96 if ((filename
!=NULL
) && (dwDesiredAccess
!= 0))
97 hFile
= CreateFile((LPCTSTR
)filename
, dwDesiredAccess
, dwShareMode
, NULL
,
98 dwCreationDisposition
, dwFlagsAndAttributes
, NULL
);
100 if (hFile
== INVALID_HANDLE_VALUE
)
105 WIN32FILE_IOWIN w32fiow
;
108 ret
= malloc(sizeof(WIN32FILE_IOWIN
));
111 else *((WIN32FILE_IOWIN
*)ret
) = w32fiow
;
117 uLong ZCALLBACK
win32_read_file_func (opaque
, stream
, buf
, size
)
126 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
128 if (!ReadFile(hFile
, buf
, size
, &ret
, NULL
))
130 DWORD dwErr
= GetLastError();
131 if (dwErr
== ERROR_HANDLE_EOF
)
133 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
140 uLong ZCALLBACK
win32_write_file_func (opaque
, stream
, buf
, size
)
149 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
152 if (!WriteFile(hFile
, buf
, size
, &ret
, NULL
))
154 DWORD dwErr
= GetLastError();
155 if (dwErr
== ERROR_HANDLE_EOF
)
157 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
163 long ZCALLBACK
win32_tell_file_func (opaque
, stream
)
170 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
173 DWORD dwSet
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
174 if (dwSet
== INVALID_SET_FILE_POINTER
)
176 DWORD dwErr
= GetLastError();
177 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
186 long ZCALLBACK
win32_seek_file_func (opaque
, stream
, offset
, origin
)
192 DWORD dwMoveMethod
=0xFFFFFFFF;
197 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
200 case ZLIB_FILEFUNC_SEEK_CUR
:
201 dwMoveMethod
= FILE_CURRENT
;
203 case ZLIB_FILEFUNC_SEEK_END
:
204 dwMoveMethod
= FILE_END
;
206 case ZLIB_FILEFUNC_SEEK_SET
:
207 dwMoveMethod
= FILE_BEGIN
;
214 DWORD dwSet
= SetFilePointer(hFile
, offset
, NULL
, dwMoveMethod
);
215 if (dwSet
== INVALID_SET_FILE_POINTER
)
217 DWORD dwErr
= GetLastError();
218 ((WIN32FILE_IOWIN
*)stream
) -> error
=(int)dwErr
;
227 int ZCALLBACK
win32_close_file_func (opaque
, stream
)
236 hFile
= ((WIN32FILE_IOWIN
*)stream
) -> hf
;
247 int ZCALLBACK
win32_error_file_func (opaque
, stream
)
254 ret
= ((WIN32FILE_IOWIN
*)stream
) -> error
;
259 void fill_win32_filefunc (pzlib_filefunc_def
)
260 zlib_filefunc_def
* pzlib_filefunc_def
;
262 pzlib_filefunc_def
->zopen_file
= win32_open_file_func
;
263 pzlib_filefunc_def
->zread_file
= win32_read_file_func
;
264 pzlib_filefunc_def
->zwrite_file
= win32_write_file_func
;
265 pzlib_filefunc_def
->ztell_file
= win32_tell_file_func
;
266 pzlib_filefunc_def
->zseek_file
= win32_seek_file_func
;
267 pzlib_filefunc_def
->zclose_file
= win32_close_file_func
;
268 pzlib_filefunc_def
->zerror_file
= win32_error_file_func
;
269 pzlib_filefunc_def
->opaque
=NULL
;